/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

/**
 * I18n helper
 */
Serum.I18n = function() {
    var _instance = null;

    /**
     * Constructor
     */
    var __construct = function() {
        return {
            /**
             * Bundles
             */
            locales:    {},
            
            /**
             * Loads locales
             * 
             * @param   {Object}    locales
             */
            load: function(locales) {
                for(var module in locales) {
                    for(var locale in locales[module]) {
                        this.locales[module.toLowerCase() +'~'+ locale] = locales[module][locale];
                    }
                }
            },

            /**
             * Translates
             *
             * @param   {String}    key
             * @param   {String}    fallbackKey
             * @return  {String}
             */
            translate: function(key, fallbackKey) {
                if (typeof this.locales[key] !== 'undefined') {
                    return this.locales[key];
                } else if (typeof fallbackKey !== 'undefined' && typeof this.locales[fallbackKey] !== 'undefined') {
                    return this.locales[fallbackKey];
                }

                return key;
            }
        }
    };

    return {
        /**
         * Returns instance
         *
         * @return  {Serum.I18n}    Instance
         */
        get: function() {
            if (_instance === null) {
                _instance = __construct();
            }
            return _instance;
        }
    };
}();

/**
 * I18n function
 *
 * @param   {String}    key     Translation expected
 * @param   {String}    fallbackKey
 * @return  {String}            Translation
 */
__ = function(key, fallbackKey) {
    return Serum.I18n.get().translate(key, fallbackKey);
}